
/* register.jsp */
//$("#reg").click(function() { register(); });
$("#bind").click(function() { registerBind(); });

/* 登入 */
$("#dologin").click(function() {
	login();
//	$(this).attr("disabled",true);
//	$(this).css("background","");
});



/* BlockUI */
//function dialog(message) {
//	$('#messageDiv').show();
//	$('#message').html(message);
//	setTimeout('$("#messageDiv").hide();', 1000);
//}
	
function dialog(message,time) {
	var windowHeight = 200;
	var windowWidth = 500;
	$('#message').html(message);
	$.blockUI({
		message : $('#messageDiv'),
		timeout : time,
		css : {
			top : ($(window).height() - windowHeight) / 2 + 'px',
			left : ($(window).width() - windowWidth) / 2 + 'px',
			width : windowWidth,
			border : '5px',
			padding : '20px',
			borderStyle : 'solid',
			borderColor : 'grey'
		}
	});
}

/* BlockUI Cancle Bottom */	
//$('#messageDiv>div').click(function() {
//	$.unblockUI();
//	return false;
//});


/* 登入 */
function login() {
	dialog("登入中...",0);
	//var postUrl = 'Login';
	var postUrl = ctx + "/doLogin";
//	var dataSend = {
//		user: $('#id').val(),
//		password: $('#pwd').val()
//	};
	var dataSend = {
			username: $('#id').val(),
			password: Base64.encode($('#pwd').val())
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,

		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if (data.success) {
				//window.location.href = ctx + "/common/index";
				window.location.href = ctx + "/partner/video/list/2";
			} else {
				dialog(data.msg,2000);
			}
//			if(data.status){
//				console.log("login step1 success " + data.step1);
//				loginSignBegin();
//			}else{
//				console.log("=:"+ postUrl +":= status fail");
//				dialog("帐号或密码错误，请再次检查。",2000);
//			}
		},

		error : function(jqXHR, textStatus, errorThrown) {
			dialog("请输入帐号密码。",2000);
			console.log("return fail");
				}
			});
}
var tempLogintaskId;
function loginSignBegin() {
	var postUrl = 'LoginSignBegin';
	var dataSend = {
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,

		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				tempLogintaskId=data.taskId;
				dialog("开始验证登入功能，请检查您的装置。",0);
				logincallbackloop();
			}else{
				console.log("=:"+ postUrl +":= status fail");
				dialog("帐号或密码错误，请再次检查。",2000);
			}
		},

		error : function(jqXHR, textStatus, errorThrown) {
			dialog("请输入帐号密码。",2000);
			console.log("return fail");
				}
			});
}

/* 迴圈验证次数 */
var callServiceCount = 30;
function logincallbackloop() {
	callServiceCount = 30;
	LoginCheck();
};
/* 验证间隔时间 */
function callLoginCheckAgain() {
	setTimeout(function() {
		if (callServiceCount > 0){
			// Do something after 5 seconds
			LoginCheck();
		}else{
			//$.unblockUI();
			console.log("login timeout");
			dialog("登录超时，请重新尝试。",2000);
		}
	}, 2000);
}

function LoginCheck() {
	var postUrl = 'LoginCheck';
	var dataSend = {
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log(postUrl + ", message: " + data.message);
			if (data.isReturn) {
				//有回复
				loginSignEnd();
			}else{
				if (callServiceCount > 0) {
					callServiceCount--;
					console.log("enter callback loop ,callServiceCount is "	+ callServiceCount);
					callLoginCheckAgain();
				}else{
					//$.unblockUI();
					console.log("login timeout");
					dialog("登入逾时，请重新尝试。",2000);
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
//			$('#message').html("资料传送错误");
//			alert("error occurred");
//			console.log("return fail");
			console.log("RegisterBindingCheck failure");
			dialog("无法登入，请重新尝试。",2000);
		}
	});
}

function loginSignEnd() {
	var postUrl = 'LoginSignEnd';
	var dataSend = {
		taskId : tempLogintaskId
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log("连接 Servlet 服务成功,"+ postUrl + " request message is " + data.message);
			if (data.signResult) {
				
				window.location.assign(data.redirectPage);
				dialog("登入成功。",0);
			}else{
				dialog("登入失败，请重新尝试。",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("registerSignEnd failure");
			dialog("无法取得登入结果，请重新尝试。",2000);
		}
	});
}

/* ================================ register.jsp ==================================== */

function register() {
	
	var postUrl = ctx + '/doRegister';
	var dataSend = {
		cId: $('#cId').val(),
		cPhone: $('#cPhone').val(),
		mobile: $('#mobile').val(),
		email: $('#email').val(),
		password: Base64.encode($('#password').val())
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,

		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if (data.success) {
				window.location.href = ctx + "/";
			} else {
				dialog(data.msg,2000);
			}
//			if(data.status){
//				console.log("success, step1 is " + data.step1);
//				window.location.assign(data.redirectPage);
//			}else{
//				console.log("failure");
//				dialog("您的帐号或密码错误，请重新尝试。",2000);
//			}
		},

		error : function(jqXHR, textStatus, errorThrown) {
			dialog("网路连线异常",2000);
			console.log("return fail");
		}
	});
	
//	var postUrl = 'RegisterStep1';
//	var dataSend = {
//		cName: $('#cName').val(),
//		cId: $('#cId').val(),
//		cAddress: $('#cAddress').val(),
//		cPhone: $('#cPhone').val(),
//		cExt: $('#cExt').val(),
//		user: $('#user').val(),
//		mobile: $('#mobile').val(),
//		email: $('#email').val(),
//		password: $('#password').val()
//	};
//	$.ajax({
//		url : postUrl,
//		type : "post",
//		dataType : "json",
//		data : dataSend,
//
//		success : function(data) {
//			console.log(data.message);
//			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
//			if(data.status){
//				console.log("success, step1 is " + data.step1);
//				window.location.assign(data.redirectPage);
//			}else{
//				console.log("failure");
//				dialog("您的帐号或密码错误，请重新尝试。",2000);
//			}
//		},
//
//		error : function(jqXHR, textStatus, errorThrown) {
//			dialog("网路连线异常",2000);
//			console.log("return fail");
//		}
//	});
}

/* 绑定 */
var tempBindtaskId;
function registerBind() {
	var postUrl = 'RegisterBinding';
//	var postUrl = 'RegisterBindId';
	var dataSend = {
		mobile: $('#mobile').val()
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,

		success : function(data) {
			console.log(data.message);
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				console.log("success, RegisterBind is " + data.step2);
				//console.log("TaskID: " + data.taskId);
				tempBindtaskId=data.taskId;
				dialog("开始验证绑定装置，请检查您的装置。",0);
				sendcallbackloop();
			}else{
				console.log(postUrl + " is failure");
				dialog("无法开始绑定，请重新尝试。",2000);
			}
		},

		error : function(jqXHR, textStatus, errorThrown) {
			dialog("网路连线异常",2000);
			console.log("return fail");
		}
	});
}

/* 迴圈验证次数 */
var callServiceCount = 30;
function sendcallbackloop() {
	callServiceCount = 30;
	registerBindCheck();
};
/* 验证间隔时间 */
function callPostLoginAgain() {
	setTimeout(function() {
		if (callServiceCount > 0){
			// Do something after 5 seconds
			registerBindCheck();
		}else{
			//$.unblockUI();
			console.log("login timeout");
			dialog("登入逾时，请重新尝试。",2000);
		}
	}, 2000);
}

function registerBindCheck() {
	var postUrl = 'RegisterBindingCheck';
	var dataSend = {
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			if (data.isReturn) {
				//有回复
				registerBindEnd();
			}else{
				if (callServiceCount > 0) {
					callServiceCount--;
					console.log("enter callback loop ,callServiceCount is "	+ callServiceCount);
					callPostLoginAgain();
				}else{
					console.log("login timeout");
					dialog("绑定逾时，请重新尝试。",2000);
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
//			$('#message').html("资料传送错误");
//			alert("error occurred");
//			console.log("return fail");
			console.log("RegisterBindingCheck failure");
			dialog("网路连线异常，无法开始绑定，请重新尝试。",2000);
		}
	});
}

function registerBindEnd() {
	var postUrl = 'RegisterBindEnd';
	var dataSend = {
		taskId : tempBindtaskId
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			if (data.bindResult) {
				sendToServlet();
				$("#step2").hide();
				$("#step3").show();
				dialog("绑定成功。",2000);
			}else{
				dialog("绑定失败，请重新尝试。",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("registerBindEnd failure");
			dialog("无法取得绑定结果，请重新尝试。",2000);
		}
	});
}

